<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    .box{
        margin: 50px auto;
        width: 700px;
        padding: 10px;
        border: 1px solid red ;

    }
    .box>textarea{
        display: inline-block;
        width: 700px;
        height: 100px;
        border: 1px solid #fa7d3c;
        /*不让用户修改文本域大小*/
        resize: none;
        outline: none;
    }
    .box>button{
        float: right;
        width: 100px;
        height: 38px;
        background-color: #fbbd9e;
        color: #ffffff;
        border-radius: 4px;
        border: none;
        outline: none;
        font-size: 18px;
    }
    ul{
        list-style: none;
       margin-top: 40px;
    }
    ul>li{
        line-height: 30px;
        border-bottom: 1px dashed #000;
    }
    ul>li>a{
        float: right;
        text-decoration: none;
    }
</style>
<body>
    <div class="box">
        <textarea></textarea>
        <div id="shan" class="hide"></div>
        <button>发布</button>
        <ul>
            <!-- <li><a href="#">删除</a></li>
            <li><a href="#">删除</a></li>
            <li><a href="#">删除</a></li>
            <li><a href="#">删除</a></li>
            <li><a href="#">删除</a></li> -->
        </ul>
    </div>
    <script>
        window.onload = function(){
            function $(selecter){
                return document.querySelectorAll("selecter");
            }
            //获取button按钮 并注册点击事件
            $('button').onclick = function(){
                var val = $('textarea').value
                if(val ===''){
                    // alert("输入内容不能为空");
                  $('#shan').className='animate__animated animate__flash show'
                  setTimeout(()=>{
                    $('#shan').className='show';
                  },600)
                    return
                }
                //动态创建li元素
                var newli = document.createElement("li")
                newli.innerHTML=val+'<a href="#">删除</a>';
                //吧创建的li放在ul的第一个位置
                $("ul").insertBefore(newli,$('ul').firstChild);
                //插入完成后 文本域为空
                $("textarea").value='';
                var alla = document.querySelectorAll("a");
                //获取所有a标签  循环遍历
                alla.forEach((item,index)=>{
                   item.onclick = function(){
                       //点击a标签 删除li 
                       //this.parentNode 拿到a的父节点li
                       this.parentNode.remove();
                   }
                })
            }
        }
    </script>
</body>
</html>